{% extends 'layout.html' %}

{% block content %}
	<div class="container">
		<div class="panel panel-default">
			<div class="panel-heading">表单</div>
			<div class="panel-body">
				<form id="formAdd">
					<div class="clearfix">
						{% for field in form %}
							<div class="col-xs-6">
								<div class="form-group" style="position: relative;margin-bottom: 20px">
									<label>{{ field.label }}</label>
									{{ field }}
									<span class="error-msg" style="color: red; position: absolute"></span>
								</div>
							</div>
						{% endfor %}
						<div class="col-xs-12">
							<button id="btnAdd" type="button" class="btn btn-primary">提交</button>
						</div>
					</div>
				</form>
			</div>
		</div>

		<div class="panel panel-default">
			<!-- Default panel contents -->
			<div class="panel-heading">
				<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
				任务列表
			</div>

			<!-- Table -->
			<table class="table table-bordered">
				<thead>
				<tr>
					<th>ID</th>
					<th>标题</th>
					<th>界别</th>
					<th>负责人</th>
					<th>操作</th>
				</tr>
				</thead>
				<tbody>
				{% for obj in queryset %}
					<tr>
						<th>{{ obj.id }}</th>
						<td>{{ obj.title }}</td>
						<td>{{ obj.get_level_display }}</td>
						<td>{{ obj.user.user_name }}</td>
						<td>
							<a href="#" class="btn btn-primary btn-xs">编辑</a>
							<a href="#" class="btn btn-danger btn-xs">删除</a>
						</td>
					</tr>
				{% endfor %}
				</tbody>
			</table>
		</div>
		<ul class="pagination">
			{{ page_str }}
		</ul>
		<hr>
		<div style="height: 1000px"></div>
		<div>
			<h1>Ajax学习</h1>

			<h2>实例</h2>
			<input id="btn1" type="button" class="btn btn-primary" value="点击1"/>

			<h2>实例2</h2>
			<input type="text" id="user" placeholder="name"/>
			<input type="text" id="age" placeholder="age"/>
			<input id="btn2" type="button" class="btn btn-primary" value="点击2"/>

			<h2>实例3</h2>
			<form id="form3">
				<input type="text" name="user" placeholder="name"/>
				<input type="text" name="age" placeholder="age"/>
				<input type="text" name="email" placeholder="email"/>
				<input type="text" name="more" placeholder="more"/>
			</form>
			<input id="btn3" type="button" class="btn btn-primary" value="点击3"/>
		</div>


	</div>
{% endblock %}

{% block js %}
	<script type="text/javascript">
        $(function () {
            // 页面框架加载完成之后代码自动执行
            bindBtn1Event();

            bindBtn2Event();

            bindBtn3Event();

            bindAddEvent();

        })

        function bindBtn1Event() {
            $("#btn1").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    data: {
                        n1: 123,
                        n2: 456
                    }, dataType: "JSON",
                    success: function (res) {
                        console.log(res.status);
                        console.log(res.data);
                    }
                })
            })
        }

        function bindBtn2Event() {
            $("#btn2").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    data: {
                        name: $("#user").val(),
                        age: $("#age").val()
                    }, dataType: "JSON",
                    success: function (res) {
                        console.log(res.status);
                        console.log(res.data);
                    }
                })
            })
        }

        function bindBtn3Event() {
            $("#btn3").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    data: $('#form3').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res.status);
                        console.log(res.data);
                    }
                })
            })
        }

        function bindAddEvent() {
            $("#btnAdd").click(function () {

                $(".error-msg").empty();

                $.ajax({
                    url: '/task/add/',
                    type: 'post',
                    data: $('#formAdd').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            alert("添加成功");
                            // js实现页面刷新
                            location.reload();
                        } else {
                            $.each(res.error, function (name, data) {
                                // console.log(name, data);
                                $("#id_" + name).next().text(data[0])
                            })
                        }
                    }
                })
            })
        }

        //function clickMe() {
        //    $.ajax({
        //        url: '/task/ajax/',
        //        type: 'post',
        //        data: {
        //            n1: 123,
        //            n2: 456
        //        }, success: function (res) {
        //            console.log(res);
        //        }
        //    })
        //}
	</script>
{% endblock %}